package com.sihan.framework.etm.controller;

import java.util.List;

import com.sihan.framework.etm.common.util.EtmWebUtil;
import com.sihan.framework.etm.vo.statistics.CurrentStateResultVO;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.framework.annotation.BusinessLog;
import com.framework.model.ApiResult;
import com.sihan.framework.etm.common.AppConfiguration;
import com.sihan.framework.etm.common.CacheOperatorInfo;
import com.sihan.framework.etm.common.CommonBiz;
import com.sihan.framework.etm.dto.CommonQueryDTO;
import com.sihan.framework.etm.entity.baseData.UseLocalModel;
import com.sihan.framework.etm.enums.CacheKeyName;
import com.sihan.framework.etm.service.CommonService;
import com.sihan.framework.etm.service.UserService;
import com.sihan.framework.etm.vo.CommonQueryResultVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "通用查询")
@RestController
@RequestMapping("/common/v0")
public class CommonController {

	@Autowired
	private CommonService commonService;

	@Autowired
	private UserService userService;
	
	@Autowired
	private AppConfiguration appConfig;
	
	@Autowired
	private CommonBiz commonBiz;

	@GetMapping("/query/pull")
	@ApiOperation(value = "通用下拉查询接口", response = CommonQueryResultVO.class)
	public ApiResult<List<CommonQueryResultVO>> query(CommonQueryDTO queryParam) {
		return ApiResult.success(commonService.selectPageForPull(queryParam));
	}
	
	@GetMapping("/query/deviceCode")
	@ApiOperation(value = "查询设备编号", response = CommonQueryResultVO.class)
	public ApiResult<List<CommonQueryResultVO>> deviceCode(CommonQueryDTO queryParam) {
		queryParam.setQueryType(1);
		return ApiResult.success(commonService.selectPageForPull(queryParam));
	}
	
	@GetMapping("/query/toolCode")
	@ApiOperation(value = "查询工器具编号", response = CommonQueryResultVO.class)
	public ApiResult<List<CommonQueryResultVO>> toolCode(CommonQueryDTO queryParam) {
		queryParam.setQueryType(2);
		return ApiResult.success(commonService.selectPageForPull(queryParam));
	}
	
	@GetMapping("/query/user")
	@ApiOperation(value = "查询用户姓名", response = CommonQueryResultVO.class)
	public ApiResult<List<CommonQueryResultVO>> user(CommonQueryDTO queryParam) {
		queryParam.setQueryType(3);
		return ApiResult.success(commonService.selectPageForPull(queryParam));
	}
	
	@GetMapping("/query/userCode")
	@ApiOperation(value = "查询用户工号", response = CommonQueryResultVO.class)
	public ApiResult<List<CommonQueryResultVO>> userCode(CommonQueryDTO queryParam) {
		queryParam.setQueryType(4);
		return ApiResult.success(commonService.selectPageForPull(queryParam));
	}
	
	@GetMapping("/query/role")
	@ApiOperation(value = "查询角色", response = CommonQueryResultVO.class)
	public ApiResult<List<CommonQueryResultVO>> role(CommonQueryDTO queryParam) {
		queryParam.setQueryType(5);
		if (EtmWebUtil.isAdmin()){
			queryParam.setQueryParam("admin");
		}
		return ApiResult.success(commonService.selectPageForPull(queryParam));
	}
	
	@GetMapping("/query/userAndPhone")
	@ApiOperation(value = "查询用户和电话", response = CommonQueryResultVO.class)
	public ApiResult<List<CommonQueryResultVO>> userAndPhone(CommonQueryDTO queryParam) {
		queryParam.setQueryType(6);
		return ApiResult.success(commonService.selectPageForPull(queryParam));
	}
	
	@GetMapping("/query/manufactor")
	@ApiOperation(value = "查询厂家", response = CommonQueryResultVO.class)
	public ApiResult<List<CommonQueryResultVO>> manufactor(CommonQueryDTO queryParam) {
		queryParam.setQueryType(7);
		return ApiResult.success(commonService.selectPageForPull(queryParam));
	}
	
	@GetMapping("/query/positionDevice")
	@ApiOperation(value = "查询定位装置", response = CommonQueryResultVO.class)
	public ApiResult<List<CommonQueryResultVO>> positionDevice(CommonQueryDTO queryParam) {
		queryParam.setQueryType(8);
		return ApiResult.success(commonService.selectPageForPull(queryParam));
	}
	
	@GetMapping("/query/typeAndModel")
	@ApiOperation(value = "查询工器具类型", response = CommonQueryResultVO.class)
	public ApiResult<List<CommonQueryResultVO>> typeAndModel(CommonQueryDTO queryParam) {
		queryParam.setQueryType(9);
		return ApiResult.success(commonService.selectPageForPull(queryParam));
	}
	
	@GetMapping("/query/purchase")
	@ApiOperation(value = "查询采购计划单", response = CommonQueryResultVO.class)
	public ApiResult<List<CommonQueryResultVO>> purchase(CommonQueryDTO queryParam) {
		queryParam.setQueryType(10);
		return ApiResult.success(commonService.selectPageForPull(queryParam));
	}
	
	@GetMapping("/query/toolClass")
	@ApiOperation(value = "查询工器具种类", response = CommonQueryResultVO.class)
	public ApiResult<List<CommonQueryResultVO>> toolClassify(CommonQueryDTO queryParam) {
		queryParam.setQueryType(12);
		return ApiResult.success(commonService.selectPageForPull(queryParam));
	}


	@GetMapping("/query/currentState")
	@ApiOperation(value = "查询领用状态下拉", response = CommonQueryResultVO.class)
	public ApiResult<List<CurrentStateResultVO>> currentState(CommonQueryDTO queryParam) {
		queryParam.setQueryType(17);
		return ApiResult.success(commonService.currentStateResultVOS(queryParam));
	}
	
	@GetMapping("/query/toolClassify")
	@ApiOperation(value = "查询工器具名称（大类名称）", response = CommonQueryResultVO.class)
	public ApiResult<List<CommonQueryResultVO>> toolClass(CommonQueryDTO queryParam) {
		queryParam.setQueryType(14);
		return ApiResult.success(commonService.selectPageForPull(queryParam));
	}
	
	@GetMapping("/query/toolStandard")
	@ApiOperation(value = "查询工器具型号", response = CommonQueryResultVO.class)
	public ApiResult<List<CommonQueryResultVO>> toolStandard(CommonQueryDTO queryParam) {
		queryParam.setQueryType(15);
		return ApiResult.success(commonService.selectPageForPull(queryParam));
	}
	
	@GetMapping("/query/useLocal")
	@ApiOperation(value = "查询保管地点", response = UseLocalModel.class)
	public ApiResult<?> useLocal() {
		return ApiResult.success(commonService.queryUseLocal());
	}
	@GetMapping("/query/taskName")
	@ApiOperation(value = "查询操作任务", response = CommonQueryResultVO.class)
	public ApiResult<List<CommonQueryResultVO>> taskName(CommonQueryDTO queryParam) {
		queryParam.setQueryType(11);
		return ApiResult.success(commonService.selectPageForPull(queryParam));
	}
	
	@GetMapping("/query/borrow-task")
	@ApiOperation(value = "查询待领取操作任务", response = CommonQueryResultVO.class)
	public ApiResult<List<CommonQueryResultVO>> borrowTask(CommonQueryDTO queryParam) {
		queryParam.setQueryType(13);
		return ApiResult.success(commonService.selectPageForPull(queryParam));
	}
	
	@GetMapping("/query/inspect-mechanism")
	@ApiOperation(value = "查询检验机构", response = CommonQueryResultVO.class)
	public ApiResult<List<CommonQueryResultVO>> inspectMechanism(CommonQueryDTO queryParam) {
		queryParam.setQueryType(16);
		return ApiResult.success(commonService.selectPageForPull(queryParam));
	}

	@ApiOperation("获取操作人个人信息")
	@GetMapping("/operator/info")
	@BusinessLog("获取操作人个人信息")
	@RequiresAuthentication
	public ApiResult<?> operatorInfo() {
		return ApiResult.success(userService.selectUserByUserId((String) CacheOperatorInfo.getMapValue(CacheKeyName.VALIDATED_USER_ID.getValue(), null)));
	}
	
	@ApiOperation("获取工器具默认名称")
	@GetMapping("/tool/default_name")
	@BusinessLog("获取工器具默认名称")
	public ApiResult<?> toolDefaultName() {
		return ApiResult.success(appConfig.getToolDefaultName());
	}
	
	@ApiOperation("跳过此步骤")
	@GetMapping("/skip/step")
	@BusinessLog("跳过此步骤")
	@RequiresAuthentication
	public ApiResult<?> skipStep(Boolean openDoor,String taskName,String taskId,
			String startTime,String endTime,String taskCode) {
		return commonBiz.skipStep(openDoor,taskName,taskId,startTime,endTime,taskCode);
	}
	
}
